package com.bwei.mypaot.home;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bwei.mypaot.R;
import com.bwei.mypaot.base.activity.BaseActivity;
import com.bwei.mypaot.home.adapter.ContactAdapter;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/2/27.
 */

public class Contacts extends BaseActivity {
    ArrayList<Person> list=new ArrayList<Person>();
    private ListView lv_contacts;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts_activity);
        initHeader();
        initWidget();
        setWidgetState();

       contact();
        ContactAdapter adapter=new ContactAdapter(Contacts.this,list);
        lv_contacts.setAdapter(adapter);
        lv_contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=getIntent();

                intent.putExtra("number",list.get(position).phone);
                intent.putExtra("name",list.get(position).name);
                setResult(2,intent);
                finish();
            }
        });

    }

    @Override
    public void initHeader() {

    }

    @Override
    public void initWidget() {
        lv_contacts = (ListView) findViewById(R.id.lv_contacts);

    }

    @Override
    public void setWidgetState() {

    }
    public void  contact() {
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
             Person person=new Person();
            person.name=name;
            person.phone=number;
            System.out.println("number:" + number + "name" + name);
            list.add(person);
        }

    }

}
